//
//  AppDelegate.m
//  MateAdv
//
//  Created by 茶 on 2023/9/6.
//

#import "AppDelegate.h"
#import "MANavViewController.h"
#import "MAHomeViewController.h"
#import "MAVerifyTool.h"

#import <IQKeyboardManager/IQKeyboardManager.h>
#import <XYIAPKit/XYIAPKit.h>
#import <XYIAPKit/XYStoreUserDefaultsPersistence.h>

#import <UMCommon/UMCommon.h>
#import <UMVerify/UMVerify.h>
#import <UMShare/UMShare.h>
#import "YDAvoidCrashKit.h"
#import "MAADManager.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [self initApp];

    return YES;
}

- (void)initApp {
#ifdef DEBUG
#else
    [YDAvoidCrash setAvoidCrashEnableMethodPrefixList:@[@"NS",@"MA"]];
    [YDAvoidCrash becomeAllEffectiveWithLogger:YES];
#endif
    if (MAUserBool(kUserAgree)) {
        [self privacyAgreeEvent];
    }
    
    MANavViewController *nav = [[MANavViewController alloc] initWithRootViewController:MAHomeViewController.new];
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = UIColor.whiteColor;
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    
}

- (void)privacyAgreeEvent {
    [self initUMCommon];
    MAWeakify(self);
    [UIDevice ma_idfaWithResult:^(NSString * _Nonnull result) {
        dispatch_async(dispatch_get_main_queue(), ^{
            MAStrongify(self);
            NSString *uuid = UIDevice.getDeviceNumber;
            ReqManager.userUUID = uuid;
            [kUserDefaults setObject:uuid forKey:kUserUUidKey];
            
            [[MAADManager sharedInstance] initADManager];
            [self _ma_initIAPTool]; //内购
        
            [[IQKeyboardManager sharedManager] setEnable:YES];
            [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
        });
    }];
}

- (void)initUMCommon {
    [UMConfigure initWithAppkey:UMKey channel:@"Apple"];
    [UMCommonHandler setVerifySDKInfo:UMVerifyKey complete:^(NSDictionary * _Nonnull resultDic) {
    }];
    [self confitUShareSettings];
    [self configUSharePlatforms];
}

- (void)confitUShareSettings
{
    /*
     * 打开图片水印
     */
    //[UMSocialGlobal shareInstance].isUsingWaterMark = YES;
    /*
     * 关闭强制验证https，可允许http图片分享，但需要在info.plist设置安全域名
     <key>NSAppTransportSecurity</key>
     <dict>
     <key>NSAllowsArbitraryLoads</key>
     <true/>
     </dict>
     */
    //[UMSocialGlobal shareInstance].isUsingHttpsWhenShareContent = NO;
        //配置微信平台的Universal Links
    //微信和QQ完整版会校验合法的universalLink，不设置会在初始化平台失败
    [UMSocialGlobal shareInstance].universalLinkDic = @{@(UMSocialPlatformType_WechatSession):@"https://zxh.xtboo.com/app",
                                                        @(UMSocialPlatformType_QQ):@"https://zxh.xtboo.com/qq_conn/102068322"};
}
- (void)configUSharePlatforms
{
    /* 设置微信的appKey和appSecret */
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:@"wxe595866579700459" appSecret:@"131d9bd89441480aa06efc7a59d68e38" redirectURL:nil];
    /*设置小程序回调app的回调*/
    [[UMSocialManager defaultManager] setLauchFromPlatform:(UMSocialPlatformType_WechatSession) completion:^(id userInfoResponse, NSError *error) {
        NSLog(@"setLauchFromPlatform:userInfoResponse:%@",userInfoResponse);
    }];
    /* 设置分享到QQ互联的appID
     * U-Share SDK为了兼容大部分平台命名，统一用appKey和appSecret进行参数设置，而QQ平台仅需将appID作为U-Share的appKey参数传进即可。
    */
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:@"102068322"/*设置QQ平台的appID*/ appSecret:nil redirectURL:nil];
}

// 支持所有iOS系统
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    //6.3的新的API调用，是为了兼容国外平台(例如:新版facebookSDK,VK等)的调用[如果用6.2的api调用会没有回调],对国内平台没有影响
    BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url sourceApplication:sourceApplication annotation:annotation];
    if (!result) {
         // 其他如支付等SDK的回调
    }
    return result;
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options
{
    //6.3的新的API调用，是为了兼容国外平台(例如:新版facebookSDK,VK等)的调用[如果用6.2的api调用会没有回调],对国内平台没有影响
    BOOL result = [[UMSocialManager defaultManager]  handleOpenURL:url options:options];
    if (!result) {
        // 其他如支付等SDK的回调
    }
    return result;
}

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler {
    if (![[UMSocialManager defaultManager] handleUniversalLink:userActivity options:nil]) {
        // 其他SDK的回调
    }
    return YES;
}

#pragma mark --✂️-- 内购工具
- (void)_ma_initIAPTool {
    // 验证票据
    [[XYStore defaultStore] registerReceiptVerifier:[MAVerifyTool shareTool]];
    [[XYStore defaultStore] registerTransactionPersistor:[XYStoreUserDefaultsPersistence shareInstance]];
}

@end
